import Tools from '@/utils/Tools.js';
const pay = (data, type, successCallback, failCallback) => {
	type = Number(type)
	console.log(type)
	switch (type) {
		case 10:
			successCallback && successCallback()
			break;
		case 20:
			// #ifdef H5
			if (Tools.isWechat()) {
				wxPayInWechat(data, successCallback, failCallback)
			} else {
				wxPayInH5(data)
			}
			// #endif
			// #ifndef H5
			uniPay('wxpay', data, successCallback, failCallback)
			// #endif
			break;
		case 30:
			// #ifdef H5
			alipayInH5(data, failCallback)
			// #endif
			// #ifndef H5
			uniPay('alipay', data, successCallback, failCallback)
			// #endif
			break;
		default:
			break;
	}
}
// #ifndef H5
// 合并聚合支付
const uniPay = (provider, data, successCallback, failCallback) => {
	uni.requestPayment({
		provider,
		orderInfo: data,
		timeStamp: data.timeStamp || '',
		nonceStr: data.nonceStr || '',
		package: data.package || '',
		signType: data.signType || 'MD5',
		paySign: data.paySign || '',
		success: () => {
			successCallback && successCallback()
		},
		fail: (e) => {
			Tools.showError('支付失败', () => {
				failCallback && failCallback(e)
			});
		}
	})
};
// #endif
// #ifdef H5



//微信H5支付
const wxPayInH5 = (data) => {
	location.href = data.mweb_url
}


// 微信支付在微信内
const wxPayInWechat = (data, successCallback, failCallback) => {
	WeixinJSBridge.invoke(
		'getBrandWCPayRequest', {
			...data
		},
		(res) => {
			if (res.err_msg == "get_brand_wcpay_request:ok") {
				successCallback && (setTimeout(() => {
					successCallback();
				}, 0));
				// 使用以上方式判断前端返回,微信团队郑重提示：
				//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
			} else {
				let msg = '支付失败';
				if (res.err_msg == "get_brand_wcpay_request:cancel") {
					msg = '用户主动取消支付';
				}
				Tools.showError(msg, () => {
					failCallback && setTimeout(() => {
						failCallback(msg)
					}, 1500)
				});

			}
		}
	);
}

// 支付宝支付h5
const alipayInH5 = (data, failCallback) => {
	console.log(data)
	// return false
	// #ifdef H5
	if (!data) {
		Tools.showError('支付环境不被支持，请稍后重试')
		failCallback && failCallback()
		return false;
	}
	// let formStr = "<form id='alipaysubmit' name='alipaysubmit' action='" + data.action;
	// formStr = formStr + "' method='POST'><input type='hidden' name='biz_content' value='" + data.biz_content;
	// formStr = formStr + "'/><input type='hidden' name='app_id' value='" + data.app_id;
	// formStr = formStr + "'/><input type='hidden' name='version' value='" + data.version;
	// formStr = formStr + "'/><input type='hidden' name='format' value='" + data.format;
	// formStr = formStr + "'/><input type='hidden' name='sign_type' value='" + data.sign_type;
	// formStr = formStr + "'/><input type='hidden' name='method' value='" + data.method;
	// formStr = formStr + "'/><input type='hidden' name='timestamp' value='" + data.timestamp;
	// formStr = formStr + "'/><input type='hidden' name='notify_url' value='" + data.notify_url;
	// formStr = formStr + "'/><input type='hidden' name='return_url' value='" + data.return_url;
	// formStr = formStr + "'/><input type='hidden' name='charset' value='" + data.charset;
	// formStr = formStr + "'/><input type='hidden' name='sign' value='" + data.sign;
	// formStr = formStr + "'/><input type='submit' value='ok' style='display:none;''></form>";
	let div = document.createElement("div");
	div.setAttribute("id", "alipayInH5");
	div.innerHTML = data; //res.data是返回的表单
	document.body.appendChild(div);
	document.forms['alipay_submit'].submit();
	document.body.removeChild(document.getElementById("alipayInH5"));
	// #endif
};
// #endif
export default {
	pay
}
